package game;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class Player extends AnimatedSprite 
{
	private LinkedList<Shoot> tiro;
	
	private static double speedConstante;//ALTERAR ESSA
	Point2D speed; //NAO ALTERAR ESSA
	int posX, posY;
	int numMinas;
	BloodBar x;
	private static float HP = 100;
	float time, time2;
	public static Point2D pos;
	int x1, x2, y1, y2;
	static Hud hud;
	boolean second;
	int intMine = 1;
	LinkedList<Mine> mina;
	int delayBomba = 1;
	boolean Ice = false;
	float tempo = 0;
	float timeWin = 0;
	float dano = 70 - (SceneManager.getLevel() * 10);
	private static boolean win = false;
	private double delayShoot = 0.3f;
	private Font font = LoadFonte.getFonte(2);
	
	public static double getSpeedConstante() {
		return speedConstante;
	}
	public static void addHP(float hp) {
		hud.bar.addHP(hp);
	}
	public static boolean isDead()
	{
		if(hud.bar.hp <=0)
			return true;
		
		else
			return false;
	}
		
	public static boolean isWin()
	{
		return win;
	}
		
	public Player(int indexImage, Point pos, Point size, Point sPos,
			Point sSize, Point animationType, float timePerFrame, boolean x)
	{
		
		super(indexImage, pos, size, sPos, sSize, animationType, timePerFrame, x);
			
		
			
		win = false;
		HP = 100;
		this.x = new BloodBar(5, (Point)this.position, new Point(0,0), HP);
		
		mina = new LinkedList<Mine>();
		speedConstante = 3;
		speed = new Point2D.Double(10000,10000);	
		tiro = new LinkedList<Shoot>();
		
		
		
		hud = new Hud(new Point(0,0), intMine);
		hud.setImage(2);
	}
	public void turnIce()
	{
		time2 += Time.getInstance().getLoopTime();
		
		if(time2 > 0.8)
			Ice=false;
		
			if(Ice)
			{
				speedConstante = 3;
				//System.out.println("Ice Activated");
			}
		
		}

	public void Update(Rectangle[] rect)
	{
		
		
		System.out.println((int)((this.getRectangle().getX() - BaseMapa.mapPos.getX())/40)
				+ "  " + (int)((this.getRectangle().getY() - BaseMapa.mapPos.getY())/40)
				);
		
		if(CapacitorManager.getInstance().capacitor1 != null && CapacitorManager.getInstance().capacitor1.coletado == true &&
				CapacitorManager.getInstance().capacitor2 != null && CapacitorManager.getInstance().capacitor2.coletado == true)
		{
			if(CapacitorManager.getInstance().baseCapacitor.getRectangle().contains(this.getRectangle()))
			{
				timeWin += Time.getInstance().getLoopTime();
				if(timeWin >= 3)
				{
					win = true;
				}
			}
			else
				timeWin = 0;
				
		}

		
		
		if(Ice)
			turnIce();
		else
		{
			speedConstante = 4;
			//System.out.println("Ice desactivated");
		}
		
		tempo += Time.getInstance().getLoopTime();
		pos = this.position;
		
		
		
		
		for(int i = 0; i < mina.size(); i++)
    	{
    		mina.get(i).Update(rect);
    		if(mina.get(i).detonada)
    			mina.remove(i);
    	}		
		
		for (int h = 0; h < tiro.size(); h++) 
		{
			tiro.get(h).update();
			
			if(tiro.get(h).position.getX() > 802 || tiro.get(h).position.getX() <-2 || tiro.get(h).position.getY()> 600 || tiro.get(h).position.getY() < -2)
			{
				tiro.remove(h);
			}
			else
			{
				for(int x= 0; x < BaseMapa.getObstaculos().length; x++)
					if(tiro.get(h).getRectangle().intersects(BaseMapa.getObstaculos()[x]))
					{
						tiro.remove(h);
						break;
					}
			}
		}
		for (int h = 0; h < tiro.size(); h++) 
		{
			for(int x= 0; x < Respawn.getEnemie().size(); x++)
				if(tiro.get(h).getRectangle().intersects(Respawn.getEnemie().get(x).getRectangle()))
				{
					//System.out.println(-tiro.get(h).dano);
					Respawn.getEnemie().get(x).addHP(-tiro.get(h).dano);
					tiro.remove(h);
					break;
				}
		}
		for(int i = 0; i < ShootManager.getInstance().enemyShoot.size(); i++)
		{
			if(ShootManager.getInstance().enemyShoot.get(i).getRectangle().intersects(this.getRectangle()))
			{
				hud.bar.addHP(-ShootManager.getInstance().enemyShoot.get(i).dano);
				ShootManager.getInstance().enemyShoot.remove(i);
			}
		}
		for(int i = 0; i < ShootManager.getInstance().iceShoot.size(); i++)
		{
			if(ShootManager.getInstance().iceShoot.get(i).getRectangle().intersects(this.getRectangle()))
			{
				hud.bar.addHP(-ShootManager.getInstance().iceShoot.get(i).dano);
				Ice = true;
				ShootManager.getInstance().iceShoot.remove(i);
				time2 = 0;
			}
		}
    	
		hud.UpdatePosition(this, this.intMine);
		hud.Update();
		
		posY = posX = 0;
		Input();
		
		
		super.Update();
	}
	
@Override
	public Point2D getPosition() 
	{
		return super.getPosition();
	}
	
	public void Input()
	{		
		//speed = new Point2D.Double(0, 0);
		if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_UP))
		{
			x1 = 0;
			x2 = 33;
			y1 = 0;
			y2 = 0;
			super.addPosition(new Point2D.Double(0, -speedConstante));
			animationType.y = 3;
			pressionado = true;
			posY = -1;
			speed = new Point2D.Double(0,-speedConstante);
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_DOWN))
		{	
			x1 = 0;
			x2 = 33;
			y1 = 35;
			y2 = 35;
			super.addPosition(new Point2D.Double(0, speedConstante));
			animationType.y = 1;
			pressionado = true;
			posY = 1;
			speed = new Point2D.Double(0,speedConstante);
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_LEFT))
		{
			x1 = 0;
			x2 = 0;
			y1 = 0;
			y2 = 30;
			super.addPosition(new Point2D.Double(-speedConstante, 0));
			animationType.y = 2;
			pressionado = true;
			posX = -1;
			speed = new Point2D.Double(-speedConstante,0);
		}
		else if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_RIGHT))
		{
			x1 = 35;
			x2 = 35;
			y1 = 0;
			y2 = 30;
			super.addPosition(new Point2D.Double(speedConstante, 0));
			animationType.y = 4;
			pressionado = true;
			posX = 1;
			speed = new Point2D.Double(speedConstante,0);
		}
		
		
		if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_Z) && tempo > 2 && intMine > 0)
		{
			
			mina.add(new Mine(new Point(
					(int)(this.position.getX() + this.dimension.x * 0.5f - 10),
					(int)(this.position.getY()+ this.dimension.y * 0.5f - 17))
			));
			tempo = 0;
			intMine --;
		}
		
		if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_X))
		{	
			
			time += Time.getInstance().getLoopTime();
			if(time > this.delayShoot && !second)
			{
				Audios.getInstance().Play(0);
				//new SoundClip(0,0);
				tiro.add(new Shoot(new Point((int)position.getX() + x1,
				(int)position.getY() + y1),
				new Point(10,10),
				new Point((int)speed.getX()*2, (int)speed.getY()*2),
				animationType.y,
				dano));
				second = true;
			}
			
			if (time > this.delayShoot*2)
			{
				Audios.getInstance().Play(0);
				//new SoundClip(0,0);
				tiro.add(new Shoot(new Point((int)position.getX() + x2,
					(int)position.getY() + y2),
					new Point(10,10),
					new Point((int)speed.getX()*2, (int)speed.getY()*2),
					animationType.y,
					dano));
				second = false;
				time = 0;
			}
		}
	}
	
	
	@Override
	public void draw(Graphics2D g2d) 
	{
		g2d.setFont(font);
		g2d.setColor(Color.WHITE);
		for(int i = 0; i < mina.size(); i++)
    	{
        	mina.get(i).draw(g2d);
    	}
		for(Shoot t : tiro)
		{
			t.draw(g2d);
		}
		super.draw(g2d);
		hud.draw(g2d);
		
		
		if(this.timeWin > 0)
		{
			FontMetrics fm = g2d.getFontMetrics();
			
			int x = (int)(4f-timeWin);
			if(x == 0) x=1;
			String txt = "" + x;
			
			int fWidth = fm.stringWidth(txt);
			g2d.drawString(txt,
					(int)(CapacitorManager.getInstance().baseCapacitor.getRectangle().getCenterX() - (fWidth*0.5f)),
					(int)CapacitorManager.getInstance().baseCapacitor.getRectangle().getCenterY() + 18);
		}
		
		
	}
	


	private static final long serialVersionUID = 1L;
}
